package com.allinpay.merchantweb.model;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.google.gson.Gson;
//import javax.persistence.Column;
//import javax.persistence.Id;
//import javax.persistence.SequenceGenerator;
//

/**
 * MyBatis用POJO基类
 * 
 * @author YM
 * 
 */
public class MyBatisBaseModel implements Serializable {
	private static final long serialVersionUID = 1L;

	//Description(value = "查找带回的值")
	private transient  String lookupId;
	//Description(value = "查找带回的名称")
	private transient String lookupName;
	//Description(value = "查找带回后的回调函数js方法名称")
	private transient String callJsName;
	//Description(value = "查找带回 的组")
	private transient String lookupGroupId;



	public String getLookupGroupId() {
		return lookupGroupId;
	}

	public void setLookupGroupId(String lookupGroupId) {
		this.lookupGroupId = lookupGroupId;
	}





	/**
	 * 用于存放POJO的列信息
	 */
	private transient static Map<Class<? extends MyBatisBaseModel>, List<String>> columnMap = new HashMap<Class<? extends MyBatisBaseModel>, List<String>>();

	private boolean isNull(String fieldname) {
		try {
			Field field = this.getClass().getDeclaredField(fieldname);
			return isNull(field);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}

		return false;
	}

	private boolean isNull(Field field) {
		try {
			field.setAccessible(true);
			return field.get(this) == null;
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		return false;
	}

	/**
	 * 用于计算类定义 需要POJO中的属性定义@Column(name)
	 * 
	 * public void caculationColumnList() { if
	 * (columnMap.containsKey(this.getClass())) return;
	 * 
	 * Field[] fields = this.getClass().getDeclaredFields(); List<String>
	 * columnList = new ArrayList<String>(fields.length);
	 * 
	 * for (Field field : fields) { if (field.isAnnotationPresent(Column.class))
	 * columnList.add(field.getName()); }
	 * 
	 * columnMap.put(this.getClass(), columnList); }
	 */

	/**
	 * 获取用于WHERE的 有值字段详细信息
	 * 
	 * @return
	 * 
	 *         public List<WhereColumnModel> getWhereColumnsNameValueType() {
	 *         Field[] fields = this.getClass().getDeclaredFields();
	 *         List<WhereColumnModel> columnList = new
	 *         ArrayList<WhereColumnModel>(fields.length);
	 * 
	 *         for (Field field : fields) { if
	 *         (field.isAnnotationPresent(Column.class) && !isNull(field))
	 *         columnList.add(new
	 *         WhereColumnModel(field.getName(),"#{"+field.getName()+"}", field
	 *         .getGenericType())); }
	 * 
	 *         return columnList; }
	 */

	/**
	 * 用于获取Insert的字段累加
	 * 
	 * @return
	 */
	public String returnInsertColumnsName() {
		StringBuilder sb = new StringBuilder();

		List<String> list = columnMap.get(this.getClass());
		int i = 0;
		for (String column : list) {
			if (isNull(column)) {
				continue;
			}

			if (i++ != 0) {
				sb.append(',');
			}
			sb.append(column);
		}
		return sb.toString();
	}

	/**
	 * 用于获取Insert的字段映射累加
	 * 
	 * @return
	 */
	public String returnInsertColumnsDefine() {
		StringBuilder sb = new StringBuilder();

		List<String> list = columnMap.get(this.getClass());
		int i = 0;
		for (String column : list) {
			if (isNull(column))
				continue;

			if (i++ != 0)
				sb.append(',');
			sb.append("#{").append(column).append('}');
		}
		return sb.toString();
	}

	/**
	 * 用于获取Update Set的字段累加
	 * 
	 * @return
	 */
	public String returnUpdateSet() {
		StringBuilder sb = new StringBuilder();

		List<String> list = columnMap.get(this.getClass());
		int i = 0;
		for (String column : list) {
			if (isNull(column))
				continue;

			if (i++ != 0)
				sb.append(',');
			sb.append(column).append("=#{").append(column).append('}');
		}
		return sb.toString();
	}

	/**
	 * 转化POJO为JSON格式
	 * 
	 * @return
	 */
	public String toJSONString() {
		Gson gson = new Gson();
		return gson.toJson(this);
	}

	/**
	 * 打印类字段信息
	 */
	@Override
	public String toString() {
		Field[] fields = this.getClass().getDeclaredFields();
		StringBuilder sb = new StringBuilder();
		sb.append('[');
		for (Field f : fields) {
			if (Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers()))
				continue;
			Object value = null;
			try {
				f.setAccessible(true);
				value = f.get(this);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			if (value != null)
				sb.append(f.getName()).append('=').append(value).append(',');
		}
		sb.append(']');

		return sb.toString();
	}

	private transient int startRecd;
	private transient int endRecd;
	/**
	 * 每页显示多少条(每页记录数)
	 */
	private  int pageSize = 10;
	/**
	 * 当前是第几页(页码)
	 */
	private  int pageNum = 1;

	private transient int pageNumShown;

	// 分页所要执行的配置文件中对应的id名称
	private transient String pageSqlIdName;
	private transient String countPageSqlIdName;
	
	
	

	public int getStartRecd() {
		return startRecd;
	}

	public void setStartRecd(int startRecd) {
		this.startRecd = startRecd;
	}

	public int getEndRecd() {
		return endRecd;
	}

	public void setEndRecd(int endRecd) {
		this.endRecd = endRecd;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setNumPerPage(int pageSize) {
		this.pageSize = pageSize;
		this.startRecd = pageSize * pageNum - pageSize;
		this.endRecd = pageSize * pageNum;
	}

	public int getPageNumShown() {
		return pageNumShown;
	}

	public void setPageNumShown(int pageNumShown) {
		this.pageNumShown = pageNumShown;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
		this.startRecd = pageSize * pageNum - pageSize;
		this.endRecd = pageSize * pageNum;
	}

	// public String getPageSqlIdName() {
	// if(StringUtils.isBlank(pageSqlIdName)){
	// return ("page_"+this.getTableName()+"_id").toUpperCase();
	// }
	// return pageSqlIdName;
	// }

	public void setPageSqlIdName(String pageSqlIdName) {
		this.pageSqlIdName = pageSqlIdName;
	}

	// public String getCountPageSqlIdName() {
	// if(StringUtils.isBlank(countPageSqlIdName)){
	// return ("count_page_"+this.getTableName()+"_id").toUpperCase();
	// }
	// return countPageSqlIdName;
	// }

	public void setCountPageSqlIdName(String countPageSqlIdName) {
		this.countPageSqlIdName = countPageSqlIdName;
	}

	public String getLookupId() {
		return lookupId;
	}

	public void setLookupId(String lookupId) {
		this.lookupId = lookupId;
	}

	public String getLookupName() {
		return lookupName;
	}

	public void setLookupName(String lookupName) {
		this.lookupName = lookupName;
	}

	public String getCallJsName() {
		return callJsName;
	}

	public void setCallJsName(String callJsName) {
		this.callJsName = callJsName;
	}

}
